package com.yj_app.qualitytest.base.mvp;

import com.lzy.okgo.OkGo;
import com.lzy.okgo.model.HttpParams;
import com.yj_app.qualitytest.base.QtApplication;
import com.yj_app.qualitytest.callback.DialogCallback;
import com.yj_app.qualitytest.common.Config;
import com.yj_app.qualitytest.common.NetConfig;
import com.yj_app.qualitytest.util.PreferencesManager;

import java.io.File;
import java.util.List;

import okhttp3.MediaType;

public class BaseModelImpl  {
    public void get(String datainterface, DialogCallback dialogCallback, String... keyvalue) {
        OkGo.get(NetConfig.BaseUrl+datainterface)
                .params(kv2Map(keyvalue))
                .headers(Config.SESSION_ID,"JSESSIONID=" + PreferencesManager.getInstance(QtApplication.getContext()).get(Config.SESSION_ID, ""))
                .execute(dialogCallback);
    }
    public void get(String datainterface, DialogCallback dialogCallback) {
        OkGo.get(NetConfig.BaseUrl+datainterface)
                .headers(Config.SESSION_ID,"JSESSIONID=" + PreferencesManager.getInstance(QtApplication.
                        getIntance()).get(Config.SESSION_ID, ""))
                .execute(dialogCallback);


    }
    public void postfile(String datainterface, DialogCallback dialogCallback, List<File> files, String... keyvalue) {
        OkGo.post(NetConfig.BaseUrl+datainterface)
                .params(kv2Map(keyvalue))
                .isMultipart(true)
                .addFileParams("signFile", files)
                .headers(Config.SESSION_ID,"JSESSIONID=" + PreferencesManager.getInstance(QtApplication.
                        getIntance()).get(Config.SESSION_ID, ""))
                .execute(dialogCallback);
    }

    public HttpParams kv2Map(String... s) {
        HttpParams params = new HttpParams();
        for (int i = 0; i < s.length; i++) {
//            i % 2 == 0 ? "偶数" : "奇数";
            if ((i + 1) % 2 != 0) {
                params.put(s[i].trim(), s[i + 1].trim());
            }
        }
        return params;
    }

    public void getMsgCommon(String json, DialogCallback<String> stringDialogCallback) {

        OkGo.<String>post(NetConfig.BASEUrl_S + NetConfig.UPDAE_SORE)//
                .tag(this).upBytes(json.getBytes(), MediaType.parse("text/xml;charset=UTF-8")).execute(stringDialogCallback);


    }
}
